package nl.hanze.designpatterns.mvc.view;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;

import nl.hanze.designpatterns.mvc.controller.TroubleTicketController;
import nl.hanze.designpatterns.mvc.model.TroubleTicketModel;

public class TroubleTicketShowAllView extends View<TroubleTicketController, TroubleTicketModel>{
	
	private JScrollPane tablePanel;
	private JTable allTicketsTable;
	private JComboBox<String> callerName;
	private JButton showAll;
	private Object[][] tickets;
	
	private String[] columnNames = {"UserName", "Caller", "Discription", "Date"};
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected TroubleTicketShowAllView(TroubleTicketController controller, TroubleTicketModel model) {
		super(controller, model);
		this.setBorder(BorderFactory.createTitledBorder("allTickets"));
		//columnNames = {"UserName", ""};
		
		this.callerName = new JComboBox<String>(this.getModel().getCallers());
		this.callerName.addActionListener(getController());
		this.callerName.setActionCommand("show_tickets_caller");
		
		this.showAll = new JButton("show all tickets");
		this.showAll.addActionListener(getController());
		this.showAll.setActionCommand("reshow_all_tickets");
		
		this.initTable();
		
		this.addRowListener(controller);
		this.tablePanel = new JScrollPane(allTicketsTable);
		tablePanel.setPreferredSize(new Dimension(750, 215));
		
		this.add(this.callerName);
		this.add(this.showAll);
		this.add(tablePanel);
		this.setVisible(true);
						
	}
	
	
	private void initTable(){
		this.tickets = getModel().convertTroubleTicketsToArray(getModel().getAllTroubleTickets());
		
		
		this.allTicketsTable = new JTable(tickets, columnNames){
			private static final long serialVersionUID = 1L;
			
			/**
			 * disable Edit Cells
			 */
			public boolean isCellEditable(int row, int column){
				return false;
			}
		};
		
		this.allTicketsTable.setCellSelectionEnabled(false);
		this.allTicketsTable.setColumnSelectionAllowed(false);
		this.allTicketsTable.setRowSelectionAllowed(true);
		this.allTicketsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		this.allTicketsTable.setShowGrid(false);
	
		
	}
	
	private void addRowListener(TroubleTicketController controller){
		MouseListener rowListener = new MouseAdapter(){
			
			@Override
			public void mousePressed(MouseEvent e){
				System.out.println("click_row_" + allTicketsTable.getSelectedRow());
				getController().setSelectedTicket(allTicketsTable.getSelectedRow());
			}
		};
		this.allTicketsTable.addMouseListener(rowListener);
	}
	
	public void showTicketsOfCaller(Object[][] tableData){
		this.updateAllTicketsTable(tableData);
	}
	
	public void tableShowAllTroubleTicketsData(){
		this.updateAllTicketsTable(getModel().convertTroubleTicketsToArray(getModel().getAllTroubleTickets()));
	}
	
	private void updateCallerSelectionBox(){
		this.callerName.removeAllItems();
		String[] callers = this.getModel().getCallers();  
		for(int i = 0; i < callers.length; i++){
			this.callerName.addItem(callers[i]);
		} 
	}
	
	
	private void updateAllTicketsTable(Object[][] tableData){
		
		
		this.tickets = tableData;
		this.allTicketsTable.setModel(new DefaultTableModel(tickets, columnNames));
		
		this.allTicketsTable.tableChanged(new TableModelEvent(this.allTicketsTable.getModel()));
		SwingUtilities.updateComponentTreeUI(this.tablePanel);
		

	}
	

	@Override
	public void notifyChange() {
		System.out.println("[ALERT] allTickets has been updated");
		this.tableShowAllTroubleTicketsData();
		this.updateCallerSelectionBox();
	}

}
